
from flask import Flask, render_template,request,escape
import requests
import pandas as pd
from PIL import Image
from io import BytesIO
from geo_weatherInfo import weatherInfo
from geo import geocode,around,staticmap
from log_sys import log_request


app = Flask(__name__)
h_key = "9283a3ae76c2742c8a55d9d6e6c75a24"

@app.route('/entry')
def entry_page() -> 'html':
    title = '欢迎使用高德API查询小功能'
    return render_template('entry.html',
                           the_title=title)


@app.route('/site', methods=['POST'])
# 这些数据来自哪里？
def do_search() -> 'html':
    # location = request.form['location']   # 客户端input form
    color01 = request.form['color01']
    color02 = request.form['color02']
    # location = request.form['location']
    用户输入的结构化地址 = request.form['geocode']
    经纬度坐标 = geocode(h_key,用户输入的结构化地址)
    周边搜素结果 = around(h_key,经纬度坐标)
    # 地址地图 = staticmap(h_key,经纬度坐标)
    position=[]
    for i,item in enumerate(用户输入的结构化地址):
        if item == '市':
            position.append(i)
    city=[]
    for item in position:
        city.append(用户输入的结构化地址[item-2:item+1])
    weather = weatherInfo(h_key,city)
    weather_i=[]
    # for i in weather:
    #     for key, value in i.items():
    #       print(key,":", value)

    title = '这里是你的结果：'

    print(request.form,request.remote_addr,request.user_agent)
    results = str(经纬度坐标)
    log_request(request,results)
    return render_template('results.html',
                           the_title=title,
                           the_color01=color01,
                           the_color02=color02,
                           the_weather=weather,
                           the_geocode=用户输入的结构化地址,
                           the_location=经纬度坐标,
                           the_around=周边搜素结果,
                           # the_staticmap=地址地图,
                           the_results=results,)


@app.route('/viewlog')
def view_the_log() -> 'html':
    contents=[]
    with open('site.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('用户提交','远程地址','访问代理','用户结果')
    return render_template('viewlog.html',
                           the_title='日志系统后台',
                           the_row_titles=titles,
                           the_data=contents)

# @app.route('/geocode',methods=['POST'])
# def do_geocode():
#     color01 = request.form['color01']
#     color02 = request.form['color02']
#     用户输入的结构化地址 = request.form['geocode']
#     经纬度坐标 = geocode(h_key,用户输入的结构化地址)
#     title = '这里是你的结果：'
#     print(request.form,request.remote_addr,request.user_agent)
#     results = str(经纬度坐标)
#     log_request(request,results)
#     return render_template('results_geocode.html',
#                            the_title=title,
#                            the_color01=color01,
#                            the_color02=color02,
#                            the_geocode=用户输入的结构化地址,
#                            the_location=经纬度坐标,
#                            the_results=results,)

@app.route('/index')
def do_index():
    title="人工智能API"
    return render_template('bootstrap.html',
                           the_title=title)

@app.route('/geoapi')
def do_geoapi():
    title = "高德API"
    return render_template('gaodeAPI.html',
                           the_title=title)

if __name__ == '__main__':
    app.run(debug=True)


